﻿using System.Web;
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace K3R.Imaging {
	public class ImageCache {
		#region Properties
		public HttpContext Context { get; set; }
		public string Path { get; set; }
		#endregion

		#region Dynamic Properties
		public string PhysicalPath {
			get {
				return Context.Server.MapPath(Path);
			}
		}
		#endregion

		#region Constructors
		public ImageCache(HttpContext context) {
			Context = context;
		}

		public ImageCache(HttpContext context, string path)
			: this(context) {
			Path = path;
		}

		public ImageCache(HttpContext context, string path, params object[] args)
			: this(context) {
			SetPath(path, args);
		}
		#endregion

		#region Path
		public void SetPath(string path, params object[] args) {
			Path = String.Format(path, args);
		}
		#endregion

		#region File System
		public bool TransferIfExists() {
			return ImageCache.TransferIfExists(Context.Server, Path);
		}
		#endregion

		#region Static
		public static bool TransferIfExists(HttpServerUtility server, string path) {
			var exists = false;
			if (File.Exists(server.MapPath(path))) {
				exists = true;
				server.Transfer(path);
			}
			return exists;
		}
		#endregion
	}
}